package com.example.trafficexam2;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.trafficexam2.dialog.NetSettingDialog;

import org.json.JSONException;
import org.json.JSONObject;

public class LoginActivity extends AppCompatActivity {

    private Button register;
    private Button netSet;
    private Button login;
    private EditText userName;
    private EditText password;
    private String result;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        register = (Button) findViewById(R.id.bt_register);
        netSet = (Button) findViewById(R.id.bt_net_setting);
        login = (Button) findViewById(R.id.bt_login);
        userName = (EditText) findViewById(R.id.et_username);
        password = (EditText) findViewById(R.id.et_password);

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                doLogin();
            }
        });

        netSet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                NetSettingDialog dialog = new NetSettingDialog(LoginActivity.this);
                dialog.setTitle("服务器IP地址设置");
                dialog.show();
            }
        });


    }



    private void doLogin() {
        String user=userName.getText().toString();
        String pwd=password.getText().toString();
        String urls = "http://192.168.1.142:8080/RestaurantServer/action/user_login.do";
        RequestQueue mQueue= Volley.newRequestQueue(getApplicationContext());
        JSONObject object=new JSONObject();
        try {
            object.put("userName",user);
            object.put("userPwd",pwd);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonObjectRequest jor=new JsonObjectRequest(Request.Method.POST, urls, object, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                result = jsonObject.optString("RESULT");
                if(result.equals("S")){

                        Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                        startActivity(intent);
                        finish();
                }else if(result.equals("F")){
                    Toast.makeText(LoginActivity.this, jsonObject.optString("ERRMSG"), Toast.LENGTH_SHORT).show();
                }
                else{
                    Toast.makeText(LoginActivity.this, "登录错误", Toast.LENGTH_SHORT).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(LoginActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
            }
        });
        mQueue.add(jor);
    }
}
